import {Component, OnInit, ViewChild} from '@angular/core';
import {CONSTANT} from "../../service/constant.service";
import {TitleCpt} from "../../lib/title/title.component";
import {ROUTE} from "../../service/route.service";
import {APARTMENT} from "../../service/apartment.service";
import {UserModel} from "../../model/user.model";
import {USER} from "../../service/user.service";
import {LoginModalViewComponent} from "../login-modal-view/login-modal-view.component";

@Component({
  selector: 'app-renting-mall-view',
  templateUrl: './renting-mall-view.component.html',
  styleUrls: ['./renting-mall-view.component.scss']
})
export class RentingMallViewComponent implements OnInit {
  @ViewChild(LoginModalViewComponent) public loginModal:any;
  showLoginModal:boolean = false;
  user:UserModel;
  goods:any={};
  goodsList:any = [];
  reqData:any = {
    page:0,
    size:999,
    type:1,
    filter:''
  };
  rentingMallTitleCPTList:Array<TitleCpt> = this.constant.genRentingMallTitleCptList();

  constructor(private constant:CONSTANT,
              private route:ROUTE,
              private apartmentApi:APARTMENT,
              private USER:USER) {
    this.user = this.USER.get();
  }

  ngOnInit() {
    // console.log(this.user);
    this.goLoginModal();
    this.search();
  }

  goLoginModal(){
    if(this.user==null){
      this.showLoginModal = true;
    }
  }
  closeLogin(result:Object){
    if(result instanceof UserModel){
      this.user=result;
      this.USER.update(result);
    }
    this.showLoginModal=false;
  }

  pageChange(type:number){
    type == 1 && this.reqData.page > 0 ? this.reqData.page-- : this.reqData.page++;
    this.search();
  }
  search(){
    let params:any = this.reqData;
    this.loadGoods(params);
  }

  loadGoods(params:any){
    console.log(params);
    this.apartmentApi.getGoodsList(params,(res:any)=>{
      console.log(res);
      this.goods = res;
      let list:any = [];
      for(let item of res.content){
        let data:any = {
          id:item.id,
          title:item.title,
          price:item.salesPrice,
          img:item.thumb?item.thumb:'assets/images/brand-story.jpg',
        }
        list.push(data);
      }
      this.goodsList = list;
      console.log(this.goodsList);
    })
  }

  goToDetailView(item:any){
    // console.log(item);
    this.route.goRentingMallDetailView(item);
  }


}
